package Messages;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class implements the librarian lending book
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageLendBook {
	public String bookid,title,id,wanted;
	int lended;
	int freeze;
	public int i,j,day,temp;
	static int row1;
	PreparedStatement ps,pl,ps1,ps3,ps4,p2;

	/**
	 * geetting the book id 
	 * @return book id 
	 */
	public String getBookid() {
		return bookid;
	}
	/**
	 * seeting the book id 
	 * @param bookid
	 */
	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	/**
	 * getting the title 
	 * @return title 
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * setting the title 
	 * @param title
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * getting I
	 * @return i 
	 */
	public int getI() {
		return i;
	}
	/**
	 * setting the i 
	 * @param i
	 */
	public void setI(int i) {
		this.i = i;
	}
	/**
	 * getting J 
	 * @returnj 
	 */
	public int getJ() {
		return j;
	}
	/**
	 * setting J 
	 * @param j
	 */
	public void setJ(int j) {
		this.j = j;
	}
	/**
	 * constractor of this class 
	 * @param client
	 */
	public MessageLendBook() {
		super();

	}

	@SuppressWarnings("deprecation")
	/**
	 * lend the specific book that is in str[2] to subscriber that is in str[1]
	 * @param str
	 */
	public String checklend(String[] str){
		String[] msg = null;
		try 
		{

			//System.out.println("lending a book");
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			ps=conn.getConnection().prepareStatement("SELECT * FROM db1.subscriber WHERE ID= ? And Status= ?;");
			ps.setString(1,str[1]);
			ps.setString(2,"0");
			ResultSet rs = ps.executeQuery(); 
			ps3=conn.getConnection().prepareStatement("SELECT * FROM db1.lending WHERE CopyId= ? AND LEND = ?;");
			ps3.setString(1,str[2]);
			ps3.setString(2,"yes");
			ResultSet rs3 = ps3.executeQuery(); 
			try {

				if (rs.next())
				{


					if(!rs3.next()){
						ps4=conn.getConnection().prepareStatement("SELECT * FROM db1.bookscopies WHERE CopyId= ?;");
						ps4.setString(1,str[2]);
						ResultSet rs21 = ps4.executeQuery(); 
						if(rs21.next())
						{

							ps1=conn.getConnection().prepareStatement("SELECT * FROM db1.books WHERE ID= ?;");
							ps1.setString(1,rs21.getString(3));
							ResultSet rs2 = ps1.executeQuery();  
							rs2.next();
							wanted=rs2.getString(10);
							//System.out.println("id:"+rs.getString(1)+"  status:"+rs.getString(5));
							//System.out.println("wanted:"+wanted);
							//pl=conn.getConnection().prepareStatement("SELECT * FROM db1.lending;");
							//ResultSet rs1 =pl.executeQuery();
							stmt = conn.getConnection().createStatement();
							ResultSet rssss= stmt.executeQuery("SELECT * FROM db1.lending;");
							row1=1;
							while(rssss.next()){
								temp=rssss.getInt(1);
								if(row1>temp)
									row1=row1;
								else{
									row1=temp;
								}
							}
							ps=conn.getConnection().prepareStatement("INSERT INTO  db1.lending (LendId,SubscriberId,CopyId,StartDate,EndDate,lend) VALUES (?,?,?,?,?,?)");
							java.util.Date today = new java.util.Date();
							java.sql.Date timestamp = new java.sql.Date(today.getTime());
							ps.setInt(1, this.row1+1);
							ps.setString(2,str[1]);
							ps.setString(3,str[2]);
							ps.setDate(4,timestamp);
							Date timestamp1 = new Date(today.getTime());
							day=timestamp.getDate();
							if(wanted.equals("Yes")){
								timestamp1.setDate(day+3);
							}
							else{
								timestamp1.setDate(day+14);
							}
							//System.out.println(timestamp);
							//System.out.println(timestamp1);
							ps.setDate(5,timestamp1);
							ps.setString(6,"yes");
							ps.executeUpdate();
							java.sql.PreparedStatement p2 = conn.getConnection().prepareStatement("DELETE from db1.reservation WHERE BookID = ? And SubscriberId = ?");
							p2.setString(1,str[2]);
							p2.setString(2,str[1]);
							p2.executeUpdate();												
							return ("booklendedcomplete");
						}
						else{
							//System.out.println("book not exist");
							return ("booknotexsist");
						}

					}
					else{

						//System.out.println("bookalreadylended");
						return ("bookalreadylended");
					}

				}
				else 
				{
					//System.out.println("subscriber is freezed cant lend a book");
					return ("freezed");

				}

			}
			catch (SQLException e)
			{	
				e.printStackTrace();
			}
		}

		catch (Exception ex )
		{
			ex.printStackTrace();
		}
		return ("fail");



	}


}
